﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmViolationEdit : FormBase, IPopupFormEdit {
        SortableSearchableBindingList<Vehicles> m_Act = new SortableSearchableBindingList<Vehicles>();

        public frmViolationEdit() {
            InitializeComponent();
        }
        void dataBind() {
            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");
            this.txtOperDate.DataBindings.Add("EditValue", rowData, "OperDate");
            this.txtEmpolyee_Name.DataBindings.Add("EditValue", rowData, "Empolyee_Name");
            this.txtTrafficPoliceTeam.DataBindings.Add("EditValue", rowData, "TrafficPoliceTeam");
            this.txtTrafficPolice.DataBindings.Add("EditValue", rowData, "TrafficPolice");
            this.txtTel.DataBindings.Add("EditValue", rowData, "Tel");
            this.txtSite.DataBindings.Add("EditValue", rowData, "Site");
            this.ckIsPenalty.DataBindings.Add("EditValue", rowData, "IsPenalty");
            this.spPenaltyNum.DataBindings.Add("EditValue", rowData, "PenaltyNum");
            this.txtPenalty.DataBindings.Add("EditValue", rowData, "Penalty");
            this.txtCause.DataBindings.Add("EditValue", rowData, "Cause");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
            
        }
        #region IPopupFormEdit Members
        private VehicleViolation rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleViolation;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleViolation>(value as VehicleViolation, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            IList<Activities> list= MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='违章'");
            if(list!=null && list.Count>0){
                rowData.Act_Code = list[0].Act_Code;
                rowData.Act_Name = list[0].Act_Name;
            }

            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;
            IList<VehicleDriverLicense> drvlist = MainHelper.GetSqlMap<VehicleDriverLicense>().GetListByWhere<VehicleDriverLicense>("  order by Empolyee_Name");
            txtEmpolyee_Name.Properties.DataSource = drvlist;
            if (rowData.VehicleNumber == "")
            {
                rowData.OperDate = DateTime.Now;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        /// <param name="nullTest"></param>
        /// <param name="cnStr"></param>
        /// <param name="post"></param>
        public void SetComboBoxData(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, SortableSearchableBindingList<Vehicles> post)
        {
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "VehicleNumber", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }

        private void buttonEdit1_Properties_Click(object sender, EventArgs e)
        {
            Ebada.Client.Platform.Selector.frmActivitiesSelector f = new Ebada.Client.Platform.Selector.frmActivitiesSelector();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.ucActivities1.bar1.Visible = false;
            f.SetCoulos();
            if (f.ShowDialog() == DialogResult.OK)
            {
                txtAct_Name.Text = f.Activitie.Act_Name;
                
                rowData.Act_Name = txtAct_Name.Text;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (txtEmpolyee_Name.Text == "")
            {
                MessageBox.Show("驾驶员名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtEmpolyee_Name.Focus();
                return;
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void txtVehicleNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
            frm.InitData_Slect("");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtVehicleNumber.Text = frm.SelectObj.VehicleNumber;
                txtVehicleNumber.ClosePopup();
            }
        }

        private void ckIsPenalty_CheckedChanged(object sender, EventArgs e)
        {
            if (ckIsPenalty.Checked == true)
            {
                spPenaltyNum.Enabled = true;
            }
            if (ckIsPenalty.Checked == false)
            {
                spPenaltyNum.Value = 0;
                spPenaltyNum.Enabled = false;
            }
        }
    }
}